package com.mxpio.erp.inventory.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.inventory.entity.LotPropConfig;
import com.mxpio.erp.common.inventory.service.LotPropConfigService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "LotPropConfigController", description = "批次配置接口")
@RestController
@RequestMapping("/erp/inventory/lotconfig/")
public class LotPropConfigController {
	
	@Autowired
	private LotPropConfigService lotPropConfigSerivce;

	@GetMapping("list")
	@Operation(summary = "批次配置列表", description = "获取批次配置列表", method = "GET")
	public Result<List<LotPropConfig>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<LotPropConfig> lotPropConfigs = lotPropConfigSerivce.list(LotPropConfig.class, criteria);
		return Result.OK(lotPropConfigs);
	}
	
	@GetMapping("page")
	@Operation(summary = "批次配置列表", description = "获取批次配置列表", method = "GET")
	public Result<Page<LotPropConfig>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<LotPropConfig> page = lotPropConfigSerivce.listPage(LotPropConfig.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("list/{id}")
	@Operation(summary = "根据id获取批次配置", description = "根据id获取批次配置", method = "GET")
	public Result<LotPropConfig> getById(@PathVariable(name = "id", required = true) String id) {
		LotPropConfig lotPropConfig = lotPropConfigSerivce.getById(LotPropConfig.class, id);
		return Result.OK(lotPropConfig);
	}
	
	@PostMapping("add")
	@Operation(summary = "新增批次配置", description = "新增批次配置", method = "POST")
	public Result<LotPropConfig> add(@RequestBody LotPropConfig lotPropConfig) {
		lotPropConfigSerivce.save(lotPropConfig);
		return Result.OK(lotPropConfig);
	}
	
	@PutMapping("edit")
	@Operation(summary = "编辑批次配置", description = "编辑批次配置（全量）", method = "PUT")
	public Result<LotPropConfig> edit(@RequestBody LotPropConfig lotPropConfig) {
		lotPropConfigSerivce.update(lotPropConfig);
		return Result.OK(lotPropConfig);
	}
	
	@DeleteMapping("remove/{bizNos}")
	@Operation(summary = "删除批次配置", description = "删除批次配置", method = "DELETE")
	public Result<LotPropConfig> remove(@PathVariable(name = "ids", required = true) String ids) {
		String id[] = ids.split(",");
		for(String key : id){
			lotPropConfigSerivce.delete(LotPropConfig.class, key);
		}
		return Result.OK();
	}
	
}
